package com.cip.master.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.velocity.app.VelocityEngine;

import com.cip.common.util.CommonUtil;
import com.cip.common.util.GridUtil;
import com.cip.master.dao.AlbumDAO;
import com.cip.master.model.Album;
import com.cip.master.model.Section;
import com.cip.master.service.AlbumService;

public class AlbumServiceImpl implements AlbumService {
	
	AlbumDAO albumDAO;
	private VelocityEngine velocityEngine;
	
	public String getAlbumGridList(Locale locale)throws Exception {
		
		String gridSource = ""; 
		
		try {
			List<Album> albumList = getAllAlbum();
			
			List<Map<String,Object>> albumArrayList = new ArrayList<Map<String,Object>>();
			
			for (Album album : albumList) {
				Map<String,Object> albumObj = new HashMap<String, Object>();
				albumObj.put("name", "<a href='/editAlbum?album.id="+album.getId()+"' style='text-decoration:underline;color:blue;'>"+album.getName()+"</a>");
				albumObj.put("status", album.getStatus());
				
				albumArrayList.add(albumObj);			
			}
			
			gridSource = generateScriptForProcess(velocityEngine, albumArrayList, locale);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return gridSource;
	}
	
	/** Set into context of grid column names and field names and it attributes
	 * 
	 * @param context
	 * @return
	 * @throws Exception
	 */
	private static String generateScriptForProcess(VelocityEngine velocityEngine, List<Map<String, Object>> albumArrayList, Locale locale) throws Exception {
		
		return GridUtil.generateScriptForProcess(velocityEngine, albumArrayList, createAlbumGridColumnMapping(locale));
	}
	
	/**
	 * Add needed column and it mapping for my instances
	 * 
	 * @param context
	 */
	private static Map<String, Object> createAlbumGridColumnMapping(Locale locale) {
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		//ResourceBundle appResourceBundle = ResourceBundle.getBundle(Constants.BUNDLE_KEY ,locale);
		//context.put("title", appResourceBundle.getString("process.myInstances"));
		context.put("title", "Section List");
		context.put("gridId", "MY_ALBUMLIST");
		context.put("needTreeStructure", true);
		//context.put("dynamicGridWidth", "300px");
		//context.put("dynamicGridHeight", "580px");
		String columnNames = "['Name','Status']";
        CommonUtil.createFieldNameList(fieldNameList, "name", "100", "center", "","false");
        CommonUtil.createFieldNameList(fieldNameList, "status", "100", "center", "", "false");

        context.put("columnNames", columnNames);
        context.put("noOfRecords", "10");
		context.put("fieldNameList", fieldNameList);
		context.put("gridHeading", "true");
		
		return context;
	}

	public AlbumDAO getAlbumDAO() {
		return albumDAO;
	}

	public void setAlbumDAO(AlbumDAO albumDAO) {
		this.albumDAO = albumDAO;
	}

	public Album saveOrUpdate(Album album) {
		return albumDAO.saveOrUpdate(album);
	}

	public List<Album> getAllAlbum() throws Exception {
		return albumDAO.getAllAlbum();
	}

	public Album getAlbum(int id) {
		return albumDAO.getAlbum(id);
	}

	public VelocityEngine getVelocityEngine() {
		return velocityEngine;
	}

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}

}
